package it.izsto.fe.bean;

import it.izsto.constants.Constants;
import it.izsto.dto.sig4.VControlloAccessi;
import it.izsto.dto.sig4.giuseppe.UserLab;
import it.izsto.dto.sig4.giuseppe.UserQuery;
import it.izsto.utility.Util;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import org.apache.log4j.Logger;

@ManagedBean(name = "dettaglioBean")
@SessionScoped
public class DettaglioBean extends BaseBean {

	protected transient final Logger log = Logger.getLogger(this.getClass());
	private static final long serialVersionUID = 1L;

	/*
	public void createPieModel() {
		pieModel = new PieChartModel();

		BigDecimal totali = getItem().getTotali();

		BigDecimal eseguiti = getItem().getEseguiti();
		pieModel.set("Eseguiti", eseguiti.intValue() * 1000 / totali.intValue());
		BigDecimal rifiutati = getItem().getRifiutati();
		pieModel.set("Rifiutati",
				rifiutati.intValue() * 1000 / totali.intValue());
		BigDecimal validati = getItem().getValidati();
		pieModel.set("Validati", validati.intValue() * 1000 / totali.intValue());
		BigDecimal incorso = getItem().getIncorso();
		pieModel.set("In corso", incorso.intValue() * 1000 / totali.intValue());
		BigDecimal noneseguibili = getItem().getNoneseguibili();
		pieModel.set("Non eseguibili",
				noneseguibili.intValue() * 1000 / totali.intValue());
		BigDecimal inviati = getItem().getInviati();
		pieModel.set("Inviati", inviati.intValue() * 1000 / totali.intValue());
	}*/

	public void doOnLoad() {
		//recupero codetable
		codeTableLab = Util.getCodeTableMap(Constants.CodeTable.LABORATORI);
		codeTableQuery = Util.getCodeTableMap(Constants.CodeTable.QUERY);
		
		//recupero laboratori e query utente
		laboratoriUtente = getServiceBean().getLaboratoriUtente(item.getUserId());
		queryUtente = getServiceBean().getqueryUtente(item.getUserId());

		//prevalorizzo laboratori e query con quelli da DB
		selectedLab = new ArrayList<String>();
		selectedQuery = new ArrayList<String>();
		for(UserLab entry: laboratoriUtente) {
			selectedLab.add(entry.getLaboratorio());
		}
		for(UserQuery entry: queryUtente) {
			selectedQuery.add(entry.getQuery());
		}
	}
	
	@PostConstruct
	public void initialize() {
	}
	
	public String aggiornaLab() {
			
		FacesContext context = FacesContext.getCurrentInstance();
		try {
			getServiceBean().aggiornaLab(item.getUserId(), selectedLab);
			context.addMessage(null, new FacesMessage("Aggiornamento effettuato con successo",  "Your message") );
		} catch(Exception e) {
			context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, e.getMessage(), "Your message") );
		}
		
			
        return "";

	}
	
	public String aggiornaQuery() {
		
		FacesContext context = FacesContext.getCurrentInstance();
		try {
			getServiceBean().aggiornaQuery(item.getUserId(), selectedQuery);
			context.addMessage(null, new FacesMessage("Aggiornamento effettuato con successo",  "Your message") );
		} catch(Exception e) {
			context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, e.getMessage(), "Your message") );
		}
		
			
        return "";

	}
	
	public String eliminaUtente() {
		try {
			getServiceBean().eliminaUtente(item.getUserId());
			FacesContext context = FacesContext.getCurrentInstance();
			context.addMessage(null, new FacesMessage("Eliminazione effettuata con successo",  "Your message") );
			return Constants.PageID.HomePortaleID + "?faces-redirect=true";
		} catch(Exception e) {
			FacesContext context = FacesContext.getCurrentInstance();
			context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, e.getMessage(), "Your message") );
		}
        
		return "";
	}
	
	private VControlloAccessi item;
	private List<String> selectedLab;
	private Map<String,String> codeTableLab;
	private List<String> selectedQuery;
	private Map<String,String> codeTableQuery;
	private List<UserLab> laboratoriUtente;
	private List<UserQuery> queryUtente;
	
	public VControlloAccessi getItem() {
		return item;
	}

	public void setItem(VControlloAccessi item) {
		this.item = item;
	}

	public List<UserLab> getLaboratoriUtente() {
		return laboratoriUtente;
	}

	public void setLaboratoriUtente(List<UserLab> laboratoriUtente) {
		this.laboratoriUtente = laboratoriUtente;
	}

	public List<UserQuery> getQueryUtente() {
		return queryUtente;
	}

	public void setQueryUtente(List<UserQuery> queryUtente) {
		this.queryUtente = queryUtente;
	}

	public Map<String,String> getCodeTableLab() {
		return codeTableLab;
	}

	public void setCodeTableLab(Map<String,String> codeTableLab) {
		this.codeTableLab = codeTableLab;
	}

	public List<String> getSelectedLab() {
		return selectedLab;
	}

	public void setSelectedLab(List<String> selectedLab) {
		this.selectedLab = selectedLab;
	}

	public List<String> getSelectedQuery() {
		return selectedQuery;
	}

	public void setSelectedQuery(List<String> selectedQuery) {
		this.selectedQuery = selectedQuery;
	}

	public Map<String,String> getCodeTableQuery() {
		return codeTableQuery;
	}

	public void setCodeTableQuery(Map<String,String> codeTableQuery) {
		this.codeTableQuery = codeTableQuery;
	}	
}
